/*
 *  Copyright 2009 David Kuhn <david.s.kuhn at gmail.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.google.code.teamtools.domain.impl;

import java.util.ArrayList;
import java.util.List;

import com.google.code.teamtools.domain.IResource;
import com.google.code.teamtools.domain.IResourceOwner;
import com.google.code.teamtools.domain.ITeam;

/**
 * Team implementation.
 * 
 * @author David Kuhn
 */
public class Team extends AbstractResource implements ITeam
{
    private List<IResource> members = new ArrayList<IResource>();

    public Team()
    {
        super();
    }

    public Team(Long id)
    {
        super(id);
    }

    public Team(String name)
    {
        super(name);
    }

    public Team(String name, List<IResource> members)
    {
        super(name);
        takeOwnership(members);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.google.code.teamtools.domain.IResourceOwner#getOwnedResources()
     */
    public List<IResource> getOwnedResources()
    {
        return members;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.google.code.teamtools.domain.IResourceOwner#isOwned(com.google.code.teamtools.domain.IResource)
     */
    public boolean isOwnerOf(IResource resource)
    {
        return members.contains(resource) && resource.getOwner().equals(this);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.google.code.teamtools.domain.IResourceOwner#relinquishOwnership(com.google.code.teamtools.domain.IResource)
     */
    public void relinquishOwnership(IResource resource)
    {
        if (members.contains(resource)) {
            members.remove(resource);
            resource.setOwner(null);
        }
    }

    /* (non-Javadoc)
     * @see com.google.code.teamtools.domain.IResourceOwner#relinquishOwnership(java.util.List)
     */
    public void relinquishOwnership(List<IResource> resources)
    {
        // Relinquish ownership of each resource in turn.
        for (IResource r : resources) {
            relinquishOwnership(r);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.google.code.teamtools.domain.IResourceOwner#takeOwnership(com.google.code.teamtools.domain.IResource)
     */
    public void takeOwnership(IResource resource)
    {
        // Tell the old resource owner that we'll take over from here.
        IResourceOwner oldOwner = resource.getOwner();
        if (oldOwner != null) {
            oldOwner.relinquishOwnership(resource);
        }

        // Inform the resource of our take-over and register them.
        resource.setOwner(this);
        members.add(resource);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.google.code.teamtools.domain.IResourceOwner#takeOwnership(java.util.List)
     */
    public void takeOwnership(List<IResource> resources)
    {
        // Take ownership of each resource in turn.
        for (IResource r : resources) {
            takeOwnership(r);
        }
    }
}
